﻿using Core.Persistence;
using Finbuckle.MultiTenant.EntityFrameworkCore.Stores.EFCoreStore;
using Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;

namespace Infrastructure.Tenant.Persistence;
public class TenantDbContext : EFCoreStoreDbContext<BoilerplateTenantInfo>
{
    public const string Schema = SchemaNames.Tenant;
    private readonly IOptions<DatabaseOptions> _settings;
    public TenantDbContext(DbContextOptions<TenantDbContext> options, 
        IOptions<DatabaseOptions> settings)
        : base(options)
    {
        AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
        _settings = settings;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        ArgumentNullException.ThrowIfNull(modelBuilder);
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<BoilerplateTenantInfo>().ToTable("Tenants", Schema);
        modelBuilder.ConfigureDateTimeForPostgreSQL(_settings.Value);
    }
}
